﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MossmanMill.DataModel;
using System.Drawing;
using MossmanMill.View;

namespace MossmanMill
{
    public class NetworkCard : Slottable
    {
        public int NetworkId { get; private set; } //Network_Id
        public string Model { get; set; } //Network_Model
        public char Type { get; private set; } //Network_Type
        public int Connection { get; private set; } //Network_Connection, a rack Id

        public NetworkCard(int id, int rackId, string model, char type, int position, int slotId = -1)
        {
            SlotType = 'N';
            NetworkId = id;
            RackId = rackId;
            Model = model;
            Type = type;
            Position = position;
            SlotId = slotId;
        }

        public override string ToString()
        {
            return Model + " " + (Type == 'U' ? "Upstream" : "Downstream");
        }

        public override string[] GetInfo()
        {
            string[] str = new string[3];
            str[0] = "Network Card";
            str[1] = Model;
            str[2] = (Type == 'U' ? "Upstream" : "Downstream");
            return str;
        }


        public void SetType(char t)
        {
            Type = t;
        }

        public void SetConnection(Rack rack)
        {
            Connection = rack == null ? -1 : rack.Id;
        }

        public override Bitmap GetDrawBitmap(Rectangle bounds, DrawType type)
        {
            var x = bounds.Left;
            var y = bounds.Top;
            var buffer = new Bitmap(bounds.Width, bounds.Height);
            var g = Graphics.FromImage(buffer);
            if (type == DrawType.Icon)
            {
                Drawing.FillOutlineRectangle(g, new Rectangle(0, 0, 32, 32), Brushes.SpringGreen, Pens.Black);
                g.DrawString(Model, Fonts.smallTimes, Brushes.Black, 0, 0);
                g.DrawString(Type + "", Fonts.smallCourier, Brushes.Black, 6, 16);
                return buffer;
            }
            var rect = new Rectangle(0, 0, 80, 160);
            Drawing.FillOutlineRectangle(g, rect, Brushes.SpringGreen, Pens.Black);
            g.DrawString("Network Card", Fonts.mediumCourier, Brushes.Black, new Rectangle(0, 0, 80, 40), Fonts.center);
            g.DrawString(Model, Fonts.mediumCourier, Brushes.Black, 4, 40, Fonts.VERTICAL);
            if (Type == 'U')
            {
                Drawing.DrawUpArrow(g, 56, 120, Brushes.WhiteSmoke);
                g.DrawString("Upstream", Fonts.mediumTimes, Brushes.Black, 60, 40, Fonts.VERTICAL);
            }
            else if (Type == 'D')
            {
                Drawing.DrawDownArrow(g, 56, 120, Brushes.WhiteSmoke);
                g.DrawString("Downstream", Fonts.mediumTimes, Brushes.Black, 60, 40, Fonts.VERTICAL);
            }
            Drawing.DrawCircuit(g, 4, 126, 0.5f);
            return buffer;
        }

        public override void draw(System.Drawing.Graphics graphics, int x, int y, float scale = 1.0f, bool drawChildren = false)
        {
            draw(graphics, new Rectangle(x, y, 81, 161), scale, drawChildren);
        }

        public override void draw(System.Drawing.Graphics graphics, Rectangle bounds, float scale = 1.0f, bool drawChildren = false)
        {
            var x = bounds.Left;
            var y = bounds.Top;
            var buffer = GetDrawBitmap(bounds, DrawType.Full);
            graphics.DrawImage(buffer, x, y, buffer.Width * scale, buffer.Height * scale);
        }

        public override bool interact(System.Drawing.Point location, int type, int x, int y, float scale = 1.0f, bool interactWithChildren = false)
        {
            return false;
        }

        public bool Equals(NetworkCard card)
        {
            return base.Equals(card) && 
                card.Model.Equals(Model) &&
                card.Type == Type &&
                card.Connection == Connection;
        }


        public NetworkCard Copy()
        {
            return new NetworkCard(NetworkId, RackId, Model, Type, Position, SlotId);
        }
    }
}
